package com.controller;

import com.api.CommonResult;
import com.entity.table.EnrollTableDef;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import com.entity.Enroll;
import com.service.EnrollService;

import java.io.Serializable;
import java.util.List;

/**
 * 账号-密码 控制层。
 *
 * @author admin
 * @since 2024-06-19
 */
@RestController
@RequestMapping("/enroll")
public class EnrollController {

    @Autowired
    private EnrollService enrollService;

    /**
     * 添加账号-密码。
     *
     * @param enroll 账号-密码
     * @return {@code true} 添加成功，{@code false} 添加失败
     */
//    @PostMapping("save")
//    public boolean save(@RequestBody Enroll enroll) {
//        return enrollService.save(enroll);
//    }

    /**
     * 根据主键删除账号-密码。
     *
     * @param id 主键
     * @return {@code true} 删除成功，{@code false} 删除失败
     */
    @DeleteMapping("remove/{id}")
    public boolean remove(@PathVariable Serializable id) {
        return enrollService.removeById(id);
    }

    /**
     * 根据主键更新账号-密码。
     *
     * @param enroll 账号-密码
     * @return {@code true} 更新成功，{@code false} 更新失败
     */
//    @PutMapping("update")
//    public boolean update(@RequestBody Enroll enroll) {
//        return enrollService.updateById(enroll);
//    }

    /**
     * 查询所有账号-密码。
     *
     * @return 所有数据
     */
//    @GetMapping("list")
//    public List<Enroll> list() {
//        return enrollService.list();
//    }

    /**
     * 根据账号-密码主键获取详细信息。
     *
     * @param id 账号-密码主键
     * @return 账号-密码详情
     */
    @GetMapping("getInfo/{id}")
    public Enroll getInfo(@PathVariable Serializable id) {
        return enrollService.getById(id);
    }

    /**
     * 分页查询账号-密码。
     *
     * @param page 分页对象
     * @return 分页对象
     */
    @GetMapping("page")
    public Page<Enroll> page(Page<Enroll> page) {
        return enrollService.page(page);
    }

    @GetMapping(value = "/list")
    public String typeList(ModelMap model, @RequestParam(value = "account", defaultValue = "") String account,
                           @RequestParam(value = "p", defaultValue = "1") int p,
                           @RequestParam(value = "pageSize", defaultValue = "10") int pageSize){
        QueryWrapper queryWrapper = QueryWrapper.create().where(EnrollTableDef.ENROLL.ACCOUNT.like(account));
        Page<Enroll> page = new Page<>();
        page.setPageSize(pageSize);
        page.setPageNumber(p);
        Page<Enroll> pageInfo = enrollService.page(page,queryWrapper);
        model.addAttribute("page",pageInfo);
        model.addAttribute("total",pageInfo.getTotalRow());
        return "back/enroll-list";
    }

    @GetMapping(value = "/addView")
    public String addUserView() { return "back/enroll-add";}

    @PostMapping("save")
    @ResponseBody
    public CommonResult save(Enroll enroll) {
        try {
            boolean r = enrollService.save(enroll);
            return CommonResult.success(r);
        } catch (Exception e) {
            e.printStackTrace();
            String msg = enroll.getAccount()+"已存在";
            return CommonResult.failed(msg);
        }

    }

    @PostMapping("update")
    @ResponseBody
    public CommonResult update(Enroll enroll) {
        try{
            boolean r = enrollService.updateById(enroll);
            return CommonResult.success(r);
        } catch (Exception e) {
            e.printStackTrace();
            String msg = enroll.getAccount()+"已存在";
            return CommonResult.failed(msg);
        }
    }

    @PostMapping("del")
    @ResponseBody
    public CommonResult del(Integer id) {
        try {
            boolean r = enrollService.removeById(id);
            return CommonResult.success(r);
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed(e.getMessage());
        }
    }

}
